package org.lzy.shop.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.lzy.shop.R
import org.lzy.shop.databinding.ItemCommunityPostBinding
import org.lzy.shop.response.CommunityPostResponse
import org.lzy.shop.util.DateUtils
import org.lzy.shop.util.setUrlRound

class CommunityPostAdapter : RecyclerView.Adapter<CommunityPostAdapter.PostViewHolder>() {

    private var postList: List<CommunityPostResponse> = emptyList()
    private var onItemClickListener: ((View, ViewGroup, Int) -> Unit)? = null
    private var onLikeClickListener: ((Int) -> Unit)? = null
    private var onCommentClickListener: ((Int) -> Unit)? = null

    fun setOnItemClickListener(listener: (View, ViewGroup, Int) -> Unit) {
        this.onItemClickListener = listener
    }

    fun setOnLikeClickListener(listener: (Int) -> Unit) {
        this.onLikeClickListener = listener
    }

    fun setOnCommentClickListener(listener: (Int) -> Unit) {
        this.onCommentClickListener = listener
    }

    // 在CommunityPostAdapter中添加
    fun appendList(list: List<CommunityPostResponse>) {
        val newList = ArrayList(postList)
        newList.addAll(list)
        this.postList = newList
        notifyDataSetChanged()
    }


    fun submitList(list: List<CommunityPostResponse>) {
        this.postList = list
        notifyDataSetChanged()
    }

    fun getItem(position: Int): CommunityPostResponse? {
        return if (position >= 0 && position < postList.size) postList[position] else null
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        val binding = ItemCommunityPostBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return PostViewHolder(binding)
    }

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        val post = postList[position]
        holder.bind(post, position)
    }

    override fun getItemCount(): Int {
        return postList.size
    }

    inner class PostViewHolder(private val binding: ItemCommunityPostBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(post: CommunityPostResponse, position: Int) {
            // 用户信息
            binding.tvUserName.text = post.userName
            binding.tvPostTime.text = DateUtils.formatTime(post.createTime)

            // 用户头像
            setUrlRound(post.userAvatar,80f,binding.ivUserAvatar)


            // 帖子内容
            binding.tvPostContent.text = post.content

            // 帖子图片
            if (post.images.isNotEmpty()) {
                binding.imageContainer.visibility = View.VISIBLE

                // 根据图片数量显示不同的布局
                when (post.images.size) {
                    1 -> {
                        binding.singleImageContainer.visibility = View.VISIBLE
                        binding.multipleImageContainer.visibility = View.GONE
                        setUrlRound(post.images[0],80f,binding.ivSingleImage)
                    }
                    2, 3 -> {
                        binding.singleImageContainer.visibility = View.GONE
                        binding.multipleImageContainer.visibility = View.VISIBLE
                        binding.ivImage1.visibility = View.VISIBLE
                        binding.ivImage2.visibility = if (post.images.size >= 2) View.VISIBLE else View.GONE
                        binding.ivImage3.visibility = View.GONE
                        setUrlRound(post.images[0],80f,binding.ivImage1)
                        if (post.images.size >= 2) {
                            setUrlRound(post.images[1],80f,binding.ivImage2)
                        }

                    }
                    else -> {
                        binding.singleImageContainer.visibility = View.GONE
                        binding.multipleImageContainer.visibility = View.VISIBLE
                        binding.ivImage1.visibility = View.VISIBLE
                        binding.ivImage2.visibility = View.VISIBLE
                        binding.ivImage3.visibility = View.VISIBLE
                        setUrlRound(post.images[0],80f,binding.ivSingleImage)
                        setUrlRound(post.images[1],80f,binding.ivSingleImage)
                        setUrlRound(post.images[2],80f,binding.ivSingleImage)

                    }
                }
            } else {
                binding.imageContainer.visibility = View.GONE
            }

            // 点赞、评论、分享数
            binding.tvLikeCount.text = post.likeCount.toString()
            binding.tvCommentCount.text = post.commentCount.toString()

            // 点赞状态
            binding.ivLike.setImageResource(if (post.isLiked) R.drawable.ic_liked else R.drawable.ic_like)
            binding.tvLikeCount.setTextColor(binding.tvLikeCount.resources.getColor(
                if (post.isLiked) R.color.colorPrimary else R.color.color_666666
            ))

            // 点击事件
            binding.root.setOnClickListener {
                onItemClickListener?.invoke(it, binding.root as ViewGroup, position)
            }

            binding.likeContainer.setOnClickListener {
                onLikeClickListener?.invoke(position)
            }

            binding.commentContainer.setOnClickListener {
                onCommentClickListener?.invoke(position)
            }


        }
    }
}